/* DISCLAIMER
Original file written by Johnny on Flame.
This piece is protected by the GPLv3 License. Commercial uses of this should only happen with my consent.
JohnnyonFlame (a_t) Hotmail (d_o_t) com
----------------------------------------------------------
This class deals with the Customization settings of the Menu

These are references, pointers and fuctions that will take care
of loading all the settings, saving them, referencing them
and other listing jobs so that they can be easily used to
build menus, skins and others.

This is the place to add more "References" and "cVars"
----------------------------------------------------------*/

#include "settings.h"
#include "menu.h"

#include "SDL/SDL.h"

#include <libxml/tree.h>

char *DEFAULT_BACKGROUND = "res/flamelauncher.png";
char *TTF_FONT = "res/Aldrich-Regular.ttf";
char *TTF_SELECTED_FONT = "res/Aldrich-Regular.ttf";
char *TTF_STATUS_FONT = "res/Aldrich-Regular.ttf";
char *BATT_SPRITE = "res/battlevel.png";

char *CMD = NULL;
char *PATH = NULL;

SDL_Event event;

int TTF_SIZE = 14;
int TTF_SELECTED_SIZE = 28;
int TTF_STATUS_SIZE = 12;

int BATT_SPRITE_COUNT = 10;
int BATT_SPRITE_WIDTH = 15;
int BATT_SPRITE_HEIGHT = 10;

SDL_Color TTF_COLOR = (SDL_Color){75,75,75};
SDL_Color TTF_STATUS_COLOR = (SDL_Color){0,0,0};
SDL_Color TTF_SELECTED_COLOR = (SDL_Color){255,255,255};


int i_FONT_ARGS[] = {0, 32}; //Min, Max FONT sizes
int i_BATT_ARGS[] = {0, 100}; //Min, Max batt args, while this is the count, I've reused it as min/max sprite size
int i_COLOUR_ARGS[] = {0, 255}; //Min, Max FONT colours
char *c_FONT_ARGS[] = {"res/","ttf",NULL}; //First one's the folder where we'll search, all others are accepted Font File Types.
char *c_IMG_ARGS[] =  {"res/","png","bmp","jpg","jpeg","gif",NULL}; //Same as c_FONT_ARGS, but for Image File Types

//List of Strings and their References.
p_Ref l_Refs[] = {
		{"DEFAULT_BACKGROUND", TYPE_FILE, &DEFAULT_BACKGROUND, &c_IMG_ARGS},

		{"BATT_SPRITE", TYPE_FILE, &BATT_SPRITE, &c_IMG_ARGS},
		{"BATT_SPRITE_COUNT", TYPE_INT, &BATT_SPRITE_COUNT, &i_BATT_ARGS},
		{"BATT_SPRITE_WIDTH", TYPE_INT, &BATT_SPRITE_WIDTH, &i_BATT_ARGS},
		{"BATT_SPRITE_HEIGHT", TYPE_INT, &BATT_SPRITE_HEIGHT, &i_BATT_ARGS},

		{"TTF_SIZE", TYPE_INT, &TTF_SIZE, &i_FONT_ARGS},
		{"TTF_SELECTED_SIZE", TYPE_INT, &TTF_SELECTED_SIZE, &i_FONT_ARGS},
		{"TTF_STATUS_SIZE", TYPE_INT, &TTF_STATUS_SIZE, &i_FONT_ARGS},

		{"TTF_FONT", TYPE_FILE, &TTF_FONT, &c_FONT_ARGS},
		{"TTF_SELECTED_FONT", TYPE_FILE, &TTF_SELECTED_FONT, &c_FONT_ARGS},
		{"TTF_STATUS_FONT", TYPE_FILE, &TTF_STATUS_FONT, &c_FONT_ARGS},

		{"TTF_COLOR_R", TYPE_INT8, &TTF_COLOR.r, &i_COLOUR_ARGS},
		{"TTF_COLOR_G", TYPE_INT8, &TTF_COLOR.g, &i_COLOUR_ARGS},
		{"TTF_COLOR_B", TYPE_INT8, &TTF_COLOR.b, &i_COLOUR_ARGS},

		{"TTF_SELECTED_COLOR_R", TYPE_INT8, &TTF_SELECTED_COLOR.r, &i_COLOUR_ARGS},
		{"TTF_SELECTED_COLOR_G", TYPE_INT8, &TTF_SELECTED_COLOR.g, &i_COLOUR_ARGS},
		{"TTF_SELECTED_COLOR_B", TYPE_INT8, &TTF_SELECTED_COLOR.b, &i_COLOUR_ARGS},

		{"TTF_STATUS_COLOR_R", TYPE_INT8, &TTF_STATUS_COLOR.r, &i_COLOUR_ARGS},
		{"TTF_STATUS_COLOR_G", TYPE_INT8, &TTF_STATUS_COLOR.g, &i_COLOUR_ARGS},
		{"TTF_STATUS_COLOR_B", TYPE_INT8, &TTF_STATUS_COLOR.b, &i_COLOUR_ARGS},

		{NULL, NULL, NULL, NULL}		
		} ;
//

p_Ref *findRefByName(char* label){
	int i=-1;
	for (;;) {
		i++; if (l_Refs[i].var == NULL) break;
		if (!strcmp(l_Refs[i].var, label))
			return(&l_Refs[i]);
	}
}


void applySetting(xmlNodePtr node){
	p_Ref *reference = findRefByName(xmlGetProp(node, "var"));
	dumpsttInformation(xmlGetProp(node, "var"),xmlGetProp(node, "value"),reference);
}

void listReferences(){ //THIS IS A DEBUG FUNCTION. I won't get into much details, it was basically used to develop stuff with the lesser ammount of possible randomness.
	int i=-1;
	for (;;) {
		i++; if (l_Refs[i].var == NULL) break;
		dumpReferenceInformation(&l_Refs[i]);
	}
}

void dumpsttInformation(char *label, char *var ,p_Ref *reference){
	if (reference->type == TYPE_INT) {
		int *a = reference->setting;
		#ifdef DEBUG
		printf("SETTING type %i; var %s,%s; ptr %p; value %i ; %s;\n", reference->type, label, reference->var, reference->setting, *a, var);
		#endif
		*a = atoi(var);
	} else
	if (reference->type == TYPE_INT8) {
		Uint8 *a = reference->setting;
		#ifdef DEBUG
		printf("SETTING type %i; var %s,%s; ptr %p; value %i ; %s;\n", reference->type, label, reference->var, reference->setting, *a, var);
		#endif
		*a = atoi(var);
	}
	if (reference->type == TYPE_FILE) {
		char **a = reference->setting;
		#ifdef DEBUG
		printf("SETTING type %i; var %s,%s; ptr %p; value %s ; %s;\n", reference->type, label, reference->var, reference->setting, *a, var);
		#endif
		*a = var;
	}
}


void dumpReferenceInformation(p_Ref *reference){
	if (reference->type == TYPE_INT) {
		int *a = reference->setting;
		printf("type %i; var %s; ptr %p; value %i;\n", reference->type, reference->var, reference->setting, *a);
	} else
	if (reference->type == TYPE_INT8) {
		Uint8 *a = reference->setting;
		printf("type %i; var %s; ptr %p; value %i;\n", reference->type, reference->var, reference->setting, *a);
	}
	if (reference->type == TYPE_FILE) {
		char **a = reference->setting;
		printf("type %i; var %s; ptr %p; value %s;\n", reference->type, reference->var, reference->setting, *a);
	}
}

